package com.lrkj.framework.websocket.dao.impl;

import com.lrkj.framework.websocket.common.UtilsConfig;
import com.lrkj.framework.websocket.dao.MessageDao;
import com.lrkj.framework.websocket.entity.WSMessage;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class MessageDaoImpl extends BaseRedisDaoImpl implements MessageDao {
    public MessageDaoImpl() {
    }

    public void cacheNotSendMessage(WSMessage message, String userId) {
        String key = this.getKey(message.getClient(), userId);
        super.cacheList(key, message, (long) UtilsConfig.MESSAGE_SAVE_TIME);
    }

    public List<WSMessage> queryNotSendMessage(String userId, String client) {
        String key = this.getKey(client, userId);
        List<WSMessage> messageList = super.getList(key, 0L, super.getListSize(key));
        super.removeList(key);
        return (List) (messageList == null ? new ArrayList() : messageList);
    }

    private String getKey(String client, String userId) {
        return "message:" + client + ":" + userId;
    }
}
